package com.example.javaapitest.testcase;

import com.example.javaapitest.common.RunCaseJson;
import com.example.javaapitest.data.DBData;
import com.example.javaapitest.data.DBData_Iterator;
import com.example.javaapitest.data.SQLProvider;
import com.example.javaapitest.pojo.ApiDataBean;
import com.example.javaapitest.util.ReadPropertiesUtil;
import org.testng.ITest;
import org.testng.annotations.*;

import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class TestFmc implements ITest {
	String getDB_IP = ReadPropertiesUtil.getProperties_1("conf.properties", "DB_IP");
	String getDB_PORT = ReadPropertiesUtil.getProperties_1("conf.properties", "DB_PORT");
	String getDB_BASE_NAME = ReadPropertiesUtil.getProperties_1("conf.properties", "DB_BASE_NAME");
	String getDB_USERNAME = ReadPropertiesUtil.getProperties_1("conf.properties", "DB_USERNAME");
	String getDB_PASSWORD = ReadPropertiesUtil.getProperties_1("conf.properties", "DB_PASSWORD");

	String sql = "SELECT * FROM testcase WHERE isRun=1 AND env='test' ORDER BY caseno;";
	int team_id = -1;
	private final ThreadLocal<String> testName = new ThreadLocal<>();

	@Parameters({"sql", "team_id"})
	@BeforeClass()
	public void beforeClass(String sql, int team_id) {
		this.sql = sql;
		this.team_id = team_id;
		// ResultRecorder.cleanInfo();
	}

	/**
	 * XML中的SQL决定了执行什么用例, 执行多少条用例, SQL的搜索结果为需要测试的测试用例
	 */
	@DataProvider(name = "DBData_Iterator")
	private Iterator<Object[]> DBData_Iterator() throws SQLException, ClassNotFoundException {
		return new DBData_Iterator(getDB_IP, getDB_PORT, getDB_BASE_NAME, getDB_USERNAME, getDB_PASSWORD, sql);
	}

	@DataProvider(name = "DBData")
	private Object[][] DBData() throws SQLException, ClassNotFoundException {
		List<Map<String, String>> result = DBData.dBData_mysql(getDB_IP, getDB_PORT, getDB_BASE_NAME, getDB_USERNAME, getDB_PASSWORD, sql);
		Object[][] files = new Object[result.size()][];
		for (int i = 0; i < result.size(); i++) {
			files[i] = new Object[]{result.get(i)};
		}
		return files;
	}

	@DataProvider(name = "testData")
	private Iterator<Object[]> testData() throws SQLException, ClassNotFoundException {
		List<Object> item = new ArrayList<Object>();

		ApiDataBean apiDataBean = new ApiDataBean();
		apiDataBean.setUrl("url");
		apiDataBean.setParameter("param");
		item.add(apiDataBean);

		ApiDataBean apiDataBean1 = new ApiDataBean();
		apiDataBean1.setUrl("url1");
		apiDataBean1.setParameter("param1");
		item.add(apiDataBean1);

		List<Object[]> users = new ArrayList<Object[]>();

		for (Object u : item) {
			//做一个形式转换
			users.add(new Object[] { u });
		}
		System.out.println(users.toString());
		return users.iterator();
	}

	@DataProvider(name = "SQLProvider")
	private Iterator<Object[]> SQLProvider() throws SQLException, ClassNotFoundException {
		return new SQLProvider(sql, getDB_IP, getDB_PORT, getDB_USERNAME, getDB_PASSWORD, getDB_BASE_NAME);
	}

	@Test(dataProvider = "SQLProvider")
	public void test(Map<String, String> data, Method method) {
		String casename = data.get("casename");
		System.out.println("当前测试方法为："+method.getName()+ getTestName());
		String requestType = data.get("method");
		String url = data.get("url");
		String api = data.get("api");
		String header = data.get("header");
		String parameter = data.get("parameter");
		String bodyString = data.get("bodyString");
		url = "http://"+ url + api;
		new RunCaseJson().runCase(requestType, url, header, parameter, bodyString);
	}

	@Override
	public String getTestName() {
		return "asd";
	}
//	@AfterMethod
//	public void afterMethod(ITestResult result, Object[] objs) {...}
//	@AfterClass
//	public void consoleLog() {...}
}
